package com.mayjean.redis.lock;

/**
 * @author
 */
public interface IRedisLock {
	long TIMEOUT_MILLIS = 2000;

	int RETRY_TIMES = 5;

	long SLEEP_MILLIS = 500;

	/**
	 * lock key
	 *
	 * @param key
	 * @return
	 */
	default boolean lock(String key) {
		return lock(key, TIMEOUT_MILLIS, RETRY_TIMES, SLEEP_MILLIS);
	}

	/**
	 * lock key retrytimes
	 *
	 * @param key
	 * @param retryTimes
	 * @return
	 */
	default boolean lock(String key, int retryTimes) {
		return lock(key, TIMEOUT_MILLIS, retryTimes, SLEEP_MILLIS);
	}

	/**
	 * lock key retryTimes sleepMillis
	 *
	 * @param key
	 * @param retryTimes
	 * @param sleepMillis
	 * @return
	 */
	default boolean lock(String key, int retryTimes, long sleepMillis) {
		return lock(key, TIMEOUT_MILLIS, retryTimes, sleepMillis);
	}

	/**
	 * lock
	 *
	 * @param key
	 * @param expireMillis
	 * @return
	 */
	default boolean lock(String key, long expireMillis) {
		return lock(key, expireMillis, RETRY_TIMES, SLEEP_MILLIS);
	}

	/**
	 * lock
	 * key expire retryTimes
	 *
	 * @param key key
	 * @param expireMillis expire
	 * @param retryTimes
	 * @return
	 */
	default boolean lock(String key, long expireMillis, int retryTimes) {
		return lock(key, expireMillis, retryTimes, SLEEP_MILLIS);
	}

	/**
	 * lock
	 *
	 * @param key         key
	 * @param expireMillis      expire
	 * @param retryTimes  retry times
	 * @param sleepMillis sleep millis
	 * @return
	 */
	boolean lock(String key, long expireMillis, int retryTimes, long sleepMillis);

	boolean releaseLock(String key);

}
